package com.atguigu.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    //业务队列业务交换机绑定：业务队列绑定死信队列(业务队列消息以后丢弃时会路由到死信队列)
    @Bean
    public Exchange test3Exchange(){
        return ExchangeBuilder.topicExchange("test3.exchange")
                .ignoreDeclarationExceptions()
                .durable(true)
                .build();
    }

    @Bean
    public Queue test3Queue(){
        return QueueBuilder.durable("test3.queue")
                .deadLetterExchange("dead.exchange")
                .deadLetterRoutingKey("dead.msg")
                .build();
    }

    @Bean
    public Binding test3Binding(Exchange test3Exchange,Queue test3Queue){
        return BindingBuilder.bind(test3Queue)
                .to(test3Exchange)
                .with("test3.*")
                .noargs();
    }


    //死信队列死信交换机 绑定
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange("dead.exchange")
                .ignoreDeclarationExceptions()
                .durable(true)
                .build();
    }

    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable("dead.queue")
//                .expires()  队列未使用被删除的时间
                //=====绑定延迟队列
                .ttl(15000) //消息未被消费的存活时间 超过时长消息会被丢弃
                .deadLetterExchange("delay.exchange")
                .deadLetterRoutingKey("delay.msg")
                .build();
    }

    @Bean
    public Binding deadBinding(Exchange deadExchange,Queue deadQueue){
        return BindingBuilder.bind(deadQueue)
                .to(deadExchange)
                .with("dead.msg")
                .noargs();
    }


    //延迟队列延迟交换机 绑定
    @Bean
    public Exchange delayExchange(){
        return ExchangeBuilder.topicExchange("delay.exchange")
                .ignoreDeclarationExceptions()
                .durable(true)
                .build();
    }

    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable("delay.queue")
                .build();
    }

    @Bean
    public Binding delayBinding(Exchange delayExchange,Queue delayQueue){
        return BindingBuilder.bind(delayQueue)
                .to(delayExchange)
                .with("delay.msg")
                .noargs();
    }
}
